package com.enpronomics.pcall.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.CheckBox;
import android.widget.EditText;

import com.enpronomics.pcall.R;
import com.enpronomics.pcall.ServiceManager;

public class AdvancedSettingsActivity extends Activity implements
		OnClickListener {

	private EditText panicSMSNumberEditText;
	private EditText panicCallNumberEditText;
	private EditText positonReportNumberEditText;
	private CheckBox autoAssistanceChB;
	private EditText autoAssistanceNumberEditText;
	private CheckBox remoteRequestChB;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.advanced_settings);

		panicSMSNumberEditText = (EditText) findViewById(R.id.Panic_SMS_Number_EditText);
		panicCallNumberEditText = (EditText) findViewById(R.id.Panic_Call_Number_EditText);
		positonReportNumberEditText = (EditText) findViewById(R.id.Position_Report_Number_EditText);
		autoAssistanceChB = (CheckBox) findViewById(R.id.auto_assistance_checkBox);
		autoAssistanceNumberEditText = (EditText) findViewById(R.id.Assistance_Number_EditText);
		remoteRequestChB = (CheckBox) findViewById(R.id.remote_request_checkBox);

		panicSMSNumberEditText.setText(ServiceManager.getStringSetting(this,
				ServiceManager.KEY_PANIC_SMS_NUMBER));
		panicCallNumberEditText.setText(ServiceManager.getStringSetting(this,
				ServiceManager.KEY_PANIC_CALL_NUMBER));
		positonReportNumberEditText.setText(ServiceManager.getStringSetting(
				this, ServiceManager.KEY_POSITION_REPORT_NUMBER));
		autoAssistanceChB.setChecked(ServiceManager.getBoolSetting(this,
				ServiceManager.KEY_AUTO_ASSISTANCE));
		autoAssistanceNumberEditText.setText(ServiceManager.getStringSetting(
				this, ServiceManager.KEY_AUTO_ASSISTANCE_NUMBER));
		remoteRequestChB.setChecked(ServiceManager.getBoolSetting(this,
				ServiceManager.KEY_REMOTE_REQUEST));

		panicSMSNumberEditText.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				if (arg0 == panicSMSNumberEditText
						&& arg1.getAction() == MotionEvent.ACTION_UP) {
					if (panicSMSNumberEditText.getText().toString()
							.equals("None"))
						panicSMSNumberEditText.setText("");
				}
				return false;
			}
		});

		panicCallNumberEditText.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				if (arg0 == panicCallNumberEditText
						&& arg1.getAction() == MotionEvent.ACTION_UP) {
					if (panicCallNumberEditText.getText().toString()
							.equals("None"))
						panicCallNumberEditText.setText("");
				}
				return false;
			}
		});

		positonReportNumberEditText.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				if (arg0 == positonReportNumberEditText
						&& arg1.getAction() == MotionEvent.ACTION_UP) {
					if (positonReportNumberEditText.getText().toString()
							.equals("None"))
						positonReportNumberEditText.setText("");
				}
				return false;
			}
		});

		autoAssistanceNumberEditText.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				if (arg0 == autoAssistanceNumberEditText
						&& arg1.getAction() == MotionEvent.ACTION_UP) {
					if (autoAssistanceNumberEditText.getText().toString()
							.equals("None"))
						autoAssistanceNumberEditText.setText("");
				}
				return false;
			}
		});

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		ServiceManager.setStringSetting(this,
				ServiceManager.KEY_PANIC_SMS_NUMBER, panicSMSNumberEditText
						.getText().toString());
		ServiceManager.setStringSetting(this,
				ServiceManager.KEY_PANIC_CALL_NUMBER, panicCallNumberEditText
						.getText().toString());
		ServiceManager.setStringSetting(this,
				ServiceManager.KEY_POSITION_REPORT_NUMBER,
				positonReportNumberEditText.getText().toString());
		ServiceManager.setBoolSetting(this, ServiceManager.KEY_AUTO_ASSISTANCE,
				autoAssistanceChB.isChecked());
		ServiceManager.setStringSetting(this,
				ServiceManager.KEY_AUTO_ASSISTANCE_NUMBER,
				autoAssistanceNumberEditText.getText().toString());
		ServiceManager.setBoolSetting(this, ServiceManager.KEY_REMOTE_REQUEST,
				remoteRequestChB.isChecked());
		super.onDestroy();
	}
}
